<?php
ini_set('display_startup_errors', true);
ini_set('display_errors', true);
error_reporting(E_ALL | E_STRICT);

define('URL', 'http://127.0.0.1/_proyek/ta-dwi-fix2');
//define('URL', 'http://127.0.0.1/myroot/_proyek/ta-dwi-fix2');
//define('URL', 'http://localhost/ta-dwi-fix2');
function base_url($url=''){
    return URL.'/'.$url;
}
function site_url($url=''){
    return URL.$url;
}
function datefmysql($tanggal){
    $pecahTanggal = explode("-", $tanggal);
    $tanggal = $pecahTanggal[2];
    $bulan   = $pecahTanggal[1];
    $tahun   = $pecahTanggal[0];
    return "$tanggal/$bulan/$tahun";                                    
}
function redirect($uri = '', $method = 'location', $http_response_code = 302)
	{
//		if ( ! preg_match('#^https?://#i', $uri))
//		{
//			$uri = site_url($uri);
//		}
                if(isset($_GET['project'])){
                    $uri.="&project=$_GET[project]";
                }
                echo'<script type="text/javascript">location.href="'.$uri.'"</script>';
//		switch($method)
//		{
//			case 'refresh'	: header("Refresh:0;url=".$uri);
//				break;
//			default			: header("Location: ".$uri, TRUE, $http_response_code);
//				break;
//		}
		exit;
	}
        
function get_menu(){
    if(!isset($_SESSION['status'])){
        return array(
            array('url'=>  site_url('/?page=home'),'display'=>'Beranda'),
            array('url'=>  site_url('/?page=help'),'display'=>'Bantuan'),
            array('url'=>  site_url('/?page=reg_view'),'display'=>'Registrasi'),
        );
    }else if($_SESSION['status']=='admin'){
        return array(
            array('url'=>  site_url('/?page=view_mapel'),'display'=>'Manajemen Mapel'),
            array('url'=>  site_url('/?page=view_pengajar'),'display'=>'Manajemen Pengajar'),
            array('url'=>  site_url('/?page=view_peserta_analisis'),'display'=>'Manajemen Peserta Analisis'),
            array('url'=>  site_url('/?page=view_peserta_adaptif'),'display'=>'Manajemen Peserta Ujian Adaptif'),
            array('url'=>  site_url('/?page=view_profil'),'display'=>'Profil Admin'),
            array('url'=>  site_url('/?page=logout&root=1'),'display'=>'Logout'),
            
        );
    }else if($_SESSION['status']=='pengajar'){
        return array(
            array('url'=>  site_url('/?page=view_paket_soal'),'display'=>'Manajemen Paket Soal'),
            array('url'=>  site_url('/?page=view_sk'),'display'=>'Manajemen Standar Kompetensi'),
            array('url'=>  site_url('/?page=view_kompetensi_dasar'),'display'=>'Manajemen Kompetensi Dasar'),
            array('url'=>  site_url('/?page=view_indikator'),'display'=>'Manajemen Indikator'),
            array('url'=>  site_url('/?page=view_profil'),'display'=>'Profil Pengajar'),
            array('url'=>  site_url('/?page=logout&root=1'),'display'=>'Logout'),
            
        );
    }else if($_SESSION['status']=='peserta_analisis'){
        return array(
            array('url'=>  site_url('/?page=beranda'),'display'=>'Beranda'),
            array('url'=>  site_url('/?page=view_profil'),'display'=>'Profilku'),
            array('url'=>  site_url('/?page=logout&root=1'),'display'=>'Logout'),
        );
    }else if($_SESSION['status']=='peserta_adaptif'){
        return array(
            array('url'=>  site_url('/?page=beranda'),'display'=>'Beranda'),
            array('url'=>  site_url('/?page=view_profil'),'display'=>'Profilku'),
            array('url'=>  site_url('/?page=logout&root=1'),'display'=>'Logout'),
        );
    }
}
function show_array($array=array()){
    echo "<pre>";
    print_r($array);
    echo "</pre>";
}
function parse_status($status){
    $status=str_replace('_', ' ', $status);
    return $status;
}
function _select_arr($sql,$is_write_sql=false) {
    if($is_write_sql)
        echo $sql.'<br>';
    $result = array();
    $exe = mysql_query($sql) or die(mysql_error() . "<hr>" . $sql);
    while ($row = mysql_fetch_array($exe)) {
        $result[] = $row;
    }
    return $result;
}

function _select($sql,$is_write_sql=false) {
    if($is_write_sql)
        echo $sql.'<br>';
    $exe = mysql_query($sql) or die(mysql_error() . "<hr>" . $sql);
    return $exe;
}

function _select_unique_result($sql,$is_write_sql=false) {
    if($is_write_sql)
        echo $sql.'<br>';
    $exe = mysql_query($sql) or die(mysql_error() . "<hr>" . $sql);
    $row = mysql_fetch_array($exe);
    return $row;
}

function div($a,$b){
    if($b==0){
        return 0;
    }else
        return (float) $a/$b;
}
function _insert($table_name,$data=array()){
    if(count($data)==0){
        echo "data kosong";exit;
    }
        
    $insertString="insert into $table_name ";
    $column="";
    $values="";
    foreach($data as $key=>$d){
        if($column==""){
            $column.="(";
            $values.="(";
        }else{
            $column.=",";
            $values.=",";
        }
        $column.="`$key`";
        $values.="'$d'";
    }
    $column.=")";
        $values.=")";
    $query=$insertString.' '.$column.' values '.$values;
    $qry=mysql_query($query) or die (mysql_error().'<br/>'.$query);
    return $qry;
}
function _update($table_name,$data,$where){
    if(count($data)==0){
        echo "data kosong";exit;
    }
        
    $insertString="update $table_name set ";
    $column="";
    foreach($data as $key=>$d){
        if($column!=""){
            $column.=",";
        }
        $column.="`$key`='$d'";
       
    }
    $query=$insertString.' '.$column.' where '.$where;
    $qry=mysql_query($query) or die (mysql_error().'<br/>'.$query);
    return $qry;
}
?>